package com.ocom.oauth.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.RequestUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.oauth.po.AlixcxPo;
import com.ocom.oauth.utils.UrlUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AliPayController {
    @PostMapping("/alixcx/login")
    public Result login(@RequestBody AlixcxPo loginPo) throws UnsupportedEncodingException {
        Map<String, Object> requstMap = new HashMap<>();
        requstMap.put("grant_type", "alixcx_login");
        requstMap.put("client_id", "client_7");
        requstMap.put("client_secret", "123456");
        requstMap.put("scene", loginPo.getScene());
        requstMap.put("code", loginPo.getCode());
        requstMap.put("comId", loginPo.getComId());
        requstMap.put("response", URLEncoder.encode(loginPo.getResponse(), "UTF-8"));
        RequestUtil requestUtil = new RequestUtil();
        String res = requestUtil.doPostForm(UrlUtil.getUrl() + "/login", requstMap);
        return JSON.parseObject(res, Result.class);
    }

    @PostMapping("/get/alixcx/user")
    public Result login() {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        System.out.printf("登录成功！userID:%s", yoCiUser.getUsername());
        return ResultUtil.success(yoCiUser);
    }

}
